package com.neusoft.crm.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neusoft.crm.mapper.ClientInfoMapper;
import com.neusoft.crm.mapper.DataClientLevelMapper;
import com.neusoft.crm.po.ClientInfo;
import com.neusoft.crm.po.DataClientLevel;
import com.neusoft.crm.po.Orders;
import com.neusoft.crm.service.DataClientLevelService;
import com.neusoft.crm.service.OrdersService;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@Log4j2
public class DataClientLevelController {

    @Resource
    private DataClientLevelMapper dataClientLevelMapper;
    @Resource
    private DataClientLevelService dataClientLevelService;
    @Resource
    private ClientInfoMapper clientInfoMapper;
    @Resource
    private OrdersService ordersService;

    @RequestMapping("/selectAllclientLevel")
    public List<DataClientLevel> selectAllclientLevel() {
        return dataClientLevelMapper.selectList(null);
    }

    @RequestMapping("/saveCustomerLevel")
    public int saveCustomerLevel(@RequestBody ClientInfo clientInfo) {
        return clientInfoMapper.updateById(clientInfo);
    }
    @RequestMapping("/countOfTradeByClientCode")
    public String countOfTradeByClientCode(@RequestBody ClientInfo clientInfo) {
        QueryWrapper<Orders> ew = new QueryWrapper<>();
        ew.eq("client_Code", clientInfo.getClientCode());
        ew.select("IFNULL(sum(orders_Total),0) AS ordersTotal");
        Map<String, Object> map = ordersService.getMap(ew);
        return JSON.toJSONString(map);
    }

    @RequestMapping("/addLevelInfoData")
    public int addLevelInfoData(@RequestBody DataClientLevel dataClientLevel){
         return dataClientLevelMapper.insert(dataClientLevel);
    }

    @RequestMapping("/deleteLevelInfoData")
    public int deleteLevelInfoData(@RequestBody DataClientLevel dataClientLevel){
        return dataClientLevelMapper.deleteById(dataClientLevel);
    }

    @RequestMapping("/updateLevelInfo")
    public int updateLevelInfo(@RequestBody DataClientLevel dataClientLevel){
        return dataClientLevelMapper.updateById(dataClientLevel);
    }
}
